package tools.SpliceSiteGui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javax.swing.WindowConstants;

import tools.blast.LongBlastHit2blastM8alignmentGenerator;
import tools.blast.blastM8Alignment;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class BlastSorterGui extends javax.swing.JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextArea BlastIn;
	private JScrollPane jScrollPane1;
	private JButton Sort;
	private JScrollPane jScrollPane2;
	private JTextArea Output;
	private JLabel jLabel2;
	private JLabel jLabel1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		BlastSorterGui inst = new BlastSorterGui();
		inst.setVisible(true);
	}
	
	public BlastSorterGui() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.0, 0.0, 0.1};
			thisLayout.rowHeights = new int[] {21, 522, 7};
			thisLayout.columnWeights = new double[] {0.0, 0.0, 0.1};
			thisLayout.columnWidths = new int[] {108, 17, 7};
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				{
					BlastIn = new JTextArea();
					jScrollPane1.setViewportView(BlastIn);
				}
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel1.setText("Input:");
			}
			{
				Sort = new JButton();
				getContentPane().add(Sort, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				Sort.setText("Sort");
				Sort.setActionCommand("sort");
				Sort.addActionListener(this);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				jLabel2.setText("Output:");
			}
			{
				jScrollPane2 = new JScrollPane();
				getContentPane().add(jScrollPane2, new GridBagConstraints(2, 1, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				{
					Output = new JTextArea();
					jScrollPane2.setViewportView(Output);
				}
			}
			pack();
			this.setSize(800, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("sort")){
			//read and sort
			String[] hits=("blaj"+BlastIn.getText()).split(" Score =");
			blastM8Alignment[] m8s=new blastM8Alignment[hits.length];
			ArrayList<Integer> order= new ArrayList<Integer>();
			for(int i=1;i<hits.length;i++){
				hits[i]=" Score ="+hits[i];
				Output.setText(hits[i]);
				m8s[i]=LongBlastHit2blastM8alignmentGenerator.LongBlastHit2blastM8alignment(hits[i]);
				int j;
				for(j=0;j<order.size();j++){
					if(m8s[i].tstart<m8s[order.get(j)].tstart){
						break;
					}
				}
				order.add(j, i);
			}
			//put together output
			String output="";
			for (Integer i : order) {
				output+=hits[i];
			}
			Output.setText(output);
		}
	}

}
